<?php

use \Entities;
use \Managers;
use \Engine\Classes;
use \Engine\Functions as FUNCTS;
use Engine\Auth;

class CodetemplatesController extends Zend_Controller_Action {

    public function init() {
        
    }

    public function indexAction() {
        
        $this->view->allTemplates = array();
        $this->view->errorMessages = array();

        if (is_file('/etc/passwd')) {
            $passwds = file_get_contents('/etc/passwd');
        }

        $allUsers = explode("\n", $passwds);
        $usersNames = array();

        foreach ($allUsers as $user) {
            if (strlen($user) > 1) {
                $tmpLine = explode(":", $user);
                $usersNames[] = $tmpLine[0];
            }
        }

        $this->view->allUsers = $usersNames;

        if ($this->_request->isPost() && $this->_request->getParam('usersNamesSelector')) {
            $userName = $this->_request->getParam('usersNamesSelector');
            $versionNetBeans = $this->_request->getParam('nbVersionSelector');

            $this->view->versionsNetBeans = FUNCTS\FileSystem::getDirectoriesList("/home/" . $userName . "/.netbeans");

            if ($this->_request->getParam('nbVersionSelector')) {
                $homeDir = "/home/" . $userName . "/.netbeans/" . $versionNetBeans . "/config/Editors/text/";
                $codeTemplateDirName = "CodeTemplates/";
                $codeTemplateFileName = "org-netbeans-modules-editor-settings-CustomCodeTemplates.xml";

                if (is_dir($homeDir)) {
                    $dirList = \Engine\Functions\FileSystem::getDirectoryTree($homeDir, 1, array(".dropbox"));

                    if ($userName && count($dirList) > 0) {
                        foreach ($dirList as $category) { // все папки с темплейтами
                            $xml = $homeDir . $category . "/" . $codeTemplateDirName . $codeTemplateFileName;
                            if (file_exists($xml)) {
                                $config = simplexml_load_file($xml);
                                foreach ($config as $codetemplate) {
                                    $tmpCT = new Entities\CodeTemplate();

                                    foreach ($codetemplate->attributes() as $attribute => $value) {
                                        if ($attribute == "abbreviation") {
                                            $tmpCT->setDescription($category);
                                            $tmpCT->setAbbreviation($value);
                                            $tmpCT->setUser(Auth\CurrentUser::getData());
                                            $tmpCT->setTemplate(nl2br(htmlspecialchars($codetemplate->code)));
                                        }
                                    }
                                    $this->view->allTemplates[$category][] = $tmpCT;
                                }
                              
                            }
                        }
                        
                        
                        
                        if($this->_getParam('name') && $this->_getParam('nbVersionSelector')){
                            $allt = $this->view->allTemplates;
                            $ctManager = new \Managers\CTemplates();
                            
                            $ctManager->saveToBase($allt);
                            
//                           $this->_redirect('./netbeans-codetemplates');
                            
                            
                        }
                        
                    } else {
                        $this->view->errorMessages[] = 'для этого пользователя ничего не обнаружено';
                    }
                } else {
                    $this->view->errorMessages[] = 'некорректная папочка пользователя';
                }
            }
        }
    }
    
    
    
    public function savetoAction(){
        
        
        
        
    }
    
    

}

